package com.haixee.parkdemo.house_resource.service.impl;

import com.haixee.parkdemo.common.context.WebContextHolder;
import com.haixee.parkdemo.house_resource.dao.HouseResourceCustomMapper;
import com.haixee.parkdemo.house_resource.dao.HouseResourceMapper;
import com.haixee.parkdemo.house_resource.entity.FieldRule;
import com.haixee.parkdemo.house_resource.entity.User;
import com.haixee.parkdemo.house_resource.feign.AuthService;
import com.haixee.parkdemo.house_resource.model.HouseResource;
import com.haixee.parkdemo.house_resource.service.HouseResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseResourceServiceImpl implements HouseResourceService {


    @Autowired
    private HouseResourceMapper houseResourceMapper;

    @Autowired
    private HouseResourceCustomMapper houseResourceCustomMapper;

    @Autowired
    private AuthService authService;

    @Override
    public void add(String name, String addr, Integer optId) {

        User user = authService.getUser(optId);
        HouseResource resource = new HouseResource();
        resource.setHouseName(name);
        resource.setHouseAddr(addr);
        resource.setOptUid(optId);
        resource.setOptOrgJob(user.getJobId()); // todo 已什么岗位进来

        houseResourceMapper.insertSelective(resource);
    }

    @Override
    public void update(Integer id, String name, String addr, Integer optId) {

        HouseResource resource = houseResourceMapper.selectByPrimaryKey(id);
        if (resource == null) {
            return;
        }

        if (!resource.getOptUid().equals(optId)) {
            return;
        }

        resource.setHouseName(name);
        resource.setHouseAddr(addr);
        houseResourceMapper.updateByPrimaryKeySelective(resource);
    }

    @Override
    public List<HouseResource> list() {

        List<FieldRule> fieldRuleList = authService.getFieldRule("t_house_resource");
        List<Integer> underTree = authService.getUnderTree(WebContextHolder.getUid());
        StringBuilder uidVal = new StringBuilder();
        for (Integer uid : underTree) {
            uidVal.append(uid).append(",");
        }
        if (uidVal.length() > 0) {
            uidVal = new StringBuilder(uidVal.substring(0, uidVal.length() - 1));
        }

        String sql = "house_addr";
        for (FieldRule fieldRule : fieldRuleList) {
            String accessWho = fieldRule.getAccessWho();
            if (!"".contentEquals(uidVal)) {
                uidVal = new StringBuilder(" or opt_org_job in (" + uidVal + ")");
            }

            if ("1".equals(accessWho)) {
                sql = "(case when (opt_uid = " + WebContextHolder.getUid() + uidVal + ") then " + fieldRule.getFieldName()
                        + " else regexp_replace(" + fieldRule.getFieldName() + ", '" + fieldRule.getRegex() + "', '" + fieldRule.getRegexData()
                        + "') end) as " + fieldRule.getFieldName();
            }
        }

        return houseResourceCustomMapper.selectAll(sql);
    }
}
